package edu.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ValueChangeEvent;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import edu.terapia.model.Cita;
import edu.terapia.model.ConducePago;
import edu.util.HbnUtil;

@ManagedBean(name="pagoBean")
@SessionScoped
public class PagoBean implements Serializable {

	public ConducePago conducePago = null;
	public List<ConducePago> conducePagos = null;
	
	@SuppressWarnings("unchecked")
	public PagoBean() {
		conducePagos = new ArrayList<ConducePago>();
		
		SessionFactory s =  HbnUtil.getSessionFactory();
		Transaction t = s.getCurrentSession().beginTransaction();
		
		load(s);
		
		t.commit();
		s.close();
	}
	
	private void load(SessionFactory s) {
		List<Cita> c = getC_CitaList(s);
		for (Iterator<Cita> iterator = c.iterator(); iterator.hasNext();) {
			Cita cita =  iterator.next();
			conducePago = new ConducePago();
			conducePago.setNombrePaciente(cita.getPaciente().getNombre() + " " + cita.getPaciente().getApellidoPaterno() + " " + cita.getPaciente().getApellidoMaterno());
			conducePago.setNombreTerapeuta(cita.getTerapeuta().getNombre() + " " + cita.getTerapeuta().getApellidoPaterno() + " " + cita.getTerapeuta().getApellidoMaterno());
			conducePago.setServicioOfrecido(cita.getServicio().getNombre() + " costo por servicio por hora:  $ " + cita.getServicio().getCosto());
			conducePago.setStatus(cita.getStatus());
			conducePago.setCitaId(cita.getId());
			conducePago.setHoraFacturadas(cita.getHorasFacturadas());
			conducePago.setCostoServicio(cita.getServicio().getCosto());
			conducePago.setTotal(cita.getHorasFacturadas() * conducePago.getCostoServicio());
			conducePagos.add(conducePago);
		}
	}
	
	public String update(ConducePago pago) {
		SessionFactory s =  HbnUtil.getSessionFactory();
		Transaction t = s.getCurrentSession().beginTransaction();
		
		this.conducePago = pago;
		
		
		t.commit();
		s.close();
		
		return "procesar_pago?faces-redirect=true";
	}
	
	public String process() {
		SessionFactory s =  HbnUtil.getSessionFactory();
		Transaction t = s.getCurrentSession().beginTransaction();
		
		s.getCurrentSession()
		.createSQLQuery("update citas set status = :status, horas_facturadas = :horas where id = :citaid")
		.setParameter("status", conducePago.getStatus())
		.setParameter("citaid", conducePago.getCitaId())
		.setParameter("horas", conducePago.getHoraFacturadas())
		.executeUpdate();
		
		conducePagos = new ArrayList<ConducePago>();
		load(s);
		
		t.commit();
		s.close();
		
		return "pagos?faces-redirect=true";
	}
	
	@SuppressWarnings("unchecked")
	private List<Cita> getC_CitaList(SessionFactory s) {
		return 
		s.getCurrentSession()
		.createSQLQuery("select c.* from citas c where c.status = 'confirmado' ")
		.addEntity(Cita.class)
		.list();
	}
	
	public void calculaHoras(ValueChangeEvent valueChangeEvent) {
		Integer i = (Integer) valueChangeEvent.getNewValue();
		this.conducePago.setTotal(i * conducePago.getCostoServicio());
	}
	
	public ConducePago getConducePago() {
		return conducePago;
	}
	public void setConducePago(ConducePago conducePago) {
		this.conducePago = conducePago;
	}
	
	public List<ConducePago> getConducePagos() {
		SessionFactory s =  HbnUtil.getSessionFactory();
		Transaction t = s.getCurrentSession().beginTransaction();
		
		conducePagos = new ArrayList<ConducePago>();
		load(s);
		
		t.commit();
		s.close();
		
		return conducePagos;
	}
	public void setConducePagos(List<ConducePago> conducePagos) {
		this.conducePagos = conducePagos;
	}
}
